Imports System.Data.Sqlclient

Friend Class DBView
    Inherits DBBase

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub Load(ByRef oObject As SQLAutoDocLib.View)
        Dim sSQL As String = "select"
        sSQL = sSQL & "     a.[RevID],"
        sSQL = sSQL & "     a.[DatabaseID],"
        sSQL = sSQL & "     a.[DatabaseRevID],"
        sSQL = sSQL & "     a.[OwnerID],"
        sSQL = sSQL & "     a.[Name],"
        sSQL = sSQL & "     a.[Desc],"
        sSQL = sSQL & "     a.[FieldsHash],"
        sSQL = sSQL & "     a.[TextHash],"
        sSQL = sSQL & "     a.[RevDate],"

        sSQL = sSQL & "     a.[IsDeleted],"
        sSQL = sSQL & "     a.[CreateName],"
        sSQL = sSQL & "     a.[CreateDate],"
        sSQL = sSQL & "     a.[ChangeName],"
        sSQL = sSQL & "     a.[ChangeDate]"
        sSQL = sSQL & " from"
        sSQL = sSQL & "     tDDViews a"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     a.[ID]=" & oObject.ID
        sSQL = sSQL & "     and a.[RevID]=("
        sSQL = sSQL & "     select"
        sSQL = sSQL & "         max(b.[RevID])"
        sSQL = sSQL & "     from"
        sSQL = sSQL & "         tDDViews b"
        sSQL = sSQL & "     where"
        sSQL = sSQL & "         b.[ID]=a.[ID])"

        Dim oData As DataTable = MyBase.ExecuteReadToDataTable(sSQL)

        LoadProperties(oObject, oData)

        oData.Dispose()

    End Sub

    Public Sub LoadRev( _
            ByRef oObject As SQLAutoDocLib.View, _
            ByVal lRevID As Long)

        Dim sSQL As String = "select"
        sSQL = sSQL & "     [RevID],"
        sSQL = sSQL & "     [DatabaseID],"
        sSQL = sSQL & "     [DatabaseRevID],"
        sSQL = sSQL & "     [OwnerID],"
        sSQL = sSQL & "     [Name],"
        sSQL = sSQL & "     [Desc],"
        sSQL = sSQL & "     [FieldsHash],"
        sSQL = sSQL & "     [TextHash],"
        sSQL = sSQL & "     [RevDate],"

        sSQL = sSQL & "     [IsDeleted],"
        sSQL = sSQL & "     [CreateName],"
        sSQL = sSQL & "     [CreateDate],"
        sSQL = sSQL & "     [ChangeName],"
        sSQL = sSQL & "     [ChangeDate]"
        sSQL = sSQL & " from"
        sSQL = sSQL & "     tDDViews"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     [ID]=" & oObject.ID
        sSQL = sSQL & "     and [RevID]=" & lRevID

        Dim oData As DataTable = MyBase.ExecuteReadToDataTable(sSQL)

        LoadProperties(oObject, oData)

        oData.Dispose()
    End Sub

    Private Sub LoadProperties( _
                    ByRef oObject As SQLAutoDocLib.View, _
                    ByVal oData As DataTable)

        With oObject
            .RevID = oData.Rows(0).Item("RevID")
            .DatabaseID = oData.Rows(0).Item("DatabaseID")
            .DatabaseRevID = oData.Rows(0).Item("DatabaseRevID")
            .OwnerID = oData.Rows(0).Item("OwnerID")
            .Name = oData.Rows(0).Item("Name")
            .Desc = NullToString(oData.Rows(0).Item("Desc"))
            .FieldsHash = FromASCII_CSV(oData.Rows(0).Item("FieldsHash"))
            .TextHash = FromASCII_CSV(oData.Rows(0).Item("TextHash"))
            .RevDate = oData.Rows(0).Item("RevDate")

            .IsDeleted = oData.Rows(0).Item("IsDeleted")
            .CreateName = oData.Rows(0).Item("CreateName")
            .CreateDate = oData.Rows(0).Item("CreateDate")
            .ChangeName = NullToString(oData.Rows(0).Item("ChangeName"))
            .ChangeDate = NullToString(oData.Rows(0).Item("ChangeDate"))
        End With
    End Sub

    Public Sub Update(ByRef oObject As SQLAutoDocLib.View)
        Dim sSQL As String = "update tDDViews set"
        sSQL = sSQL & "     [Name]=" & ValueToDBValue(oObject.Name) & ","
        sSQL = sSQL & "     [Desc]=" & ValueToDBValue(oObject.Desc) & ","
        sSQL = sSQL & "     [FieldsHash]=" & ToASCII_CSV(oObject.FieldsHash) & ","
        sSQL = sSQL & "     [TextHash]=" & ToASCII_CSV(oObject.TextHash) & ","
        sSQL = sSQL & "     [DatabaseID]=" & ValueToDBValue(oObject.DatabaseID) & ","
        sSQL = sSQL & "     [DatabaseRevID]=" & ValueToDBValue(oObject.DatabaseRevID) & ","
        sSQL = sSQL & "     [OwnerID]=" & ValueToDBValue(oObject.OwnerID) & ","
        sSQL = sSQL & "     [RevDate]=" & ValueToDBValue(oObject.RevDate) & ","

        sSQL = sSQL & "     [IsDeleted]=" & BooleanToBit(oObject.IsDeleted) & ","
        sSQL = sSQL & "     [CreateName]=" & ValueToDBValue(oObject.CreateName) & ","
        sSQL = sSQL & "     [CreateDate]=" & ValueToDBValue(oObject.CreateDate) & ","
        sSQL = sSQL & "     [ChangeName]=" & ValueToDBValue(oObject.ChangeName) & ","
        sSQL = sSQL & "     [ChangeDate]=" & ValueToDBValue(oObject.ChangeDate)
        sSQL = sSQL & " where"
        sSQL = sSQL & "     [ID]=" & oObject.ID
        sSQL = sSQL & "     and [RevID]=" & oObject.RevID

        MyBase.ExecuteNonQuery(sSQL)
    End Sub

    Public Sub Add(ByRef oObject As SQLAutoDocLib.View)
        Dim sSQL As String = "insert into tDDViews ("
        sSQL = sSQL & "     [ID],"
        sSQL = sSQL & "     [RevID],"
        sSQL = sSQL & "     [Name],"
        sSQL = sSQL & "     [Desc],"
        sSQL = sSQL & "     [FieldsHash],"
        sSQL = sSQL & "     [TextHash],"
        sSQL = sSQL & "     [DatabaseID],"
        sSQL = sSQL & "     [DatabaseRevID],"
        sSQL = sSQL & "     [OwnerID],"
        sSQL = sSQL & "     [RevDate],"

        sSQL = sSQL & "     [IsDeleted],"
        sSQL = sSQL & "     [CreateName],"
        sSQL = sSQL & "     [CreateDate],"
        sSQL = sSQL & "     [ChangeName],"
        sSQL = sSQL & "     [ChangeDate]) values ("

        sSQL = sSQL & ValueToDBValue(oObject.ID) & ","
        sSQL = sSQL & ValueToDBValue(oObject.RevID) & ","
        sSQL = sSQL & ValueToDBValue(oObject.Name) & ","
        sSQL = sSQL & ValueToDBValue(oObject.Desc) & ","
        sSQL = sSQL & ToASCII_CSV(oObject.FieldsHash) & ","
        sSQL = sSQL & ToASCII_CSV(oObject.TextHash) & ","
        sSQL = sSQL & ValueToDBValue(oObject.DatabaseID) & ","
        sSQL = sSQL & ValueToDBValue(oObject.DatabaseRevID) & ","
        sSQL = sSQL & ValueToDBValue(oObject.OwnerID) & ","
        sSQL = sSQL & ValueToDBValue(oObject.RevDate) & ","

        sSQL = sSQL & BooleanToBit(oObject.IsDeleted) & ","
        sSQL = sSQL & ValueToDBValue(oObject.CreateName) & ","
        sSQL = sSQL & ValueToDBValue(oObject.CreateDate) & ","
        sSQL = sSQL & ValueToDBValue(oObject.ChangeName) & ","
        sSQL = sSQL & ValueToDBValue(oObject.ChangeDate) & ")"

        MyBase.ExecuteNonQuery(sSQL)
    End Sub

    Public Sub Delete(ByRef oObject As SQLAutoDocLib.View)
        Dim sSQL As String = "delete from tDDViews"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     [ID]=" & oObject.ID
        sSQL = sSQL & "     and [RevID]=" & oObject.RevID

        MyBase.ExecuteNonQuery(sSQL)
    End Sub
End Class
